package com.rovinj_server.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.rovinj_server.R;
import com.rovinj_server.adapter.TaskSkedsubAdapter;
import com.rovinj_server.db.DBManager;
import com.rovinj_server.entity.InventoryTaskSub;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/5/8.
 */

public class TaskSubActivity extends AutoLayoutActivity {

   public  static final String TAG ="-----------TaskSubActivity";
    @Bind(R.id.list_view)
    ListView mListView;

    public boolean tyme=true;
    private List<InventoryTaskSub> mListInventory = new ArrayList<>();
    private List<InventoryTaskSub> mListOk = new ArrayList<>();
    private List<InventoryTaskSub> mListNo = new ArrayList<>();
    private TaskSkedsubAdapter mSkedsubAdapter;
    private int mIntID;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_sub);
        ButterKnife.bind(this);
        hidStater();
        intiData();
    }

    /**
     * 隐藏状态栏
     */
    private void hidStater() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
    }

    private void intiData() {
        mListInventory.clear();
        mListOk.clear();
        mListNo.clear();
        Intent intent = getIntent();
        if (intent != null) {
            mIntID = (int) intent.getSerializableExtra("id");
            DBManager dbManager = new DBManager(TaskSubActivity.this);
            dbManager.copyDBFile();
            mListInventory = dbManager.findTaskSubListData(mIntID);
            for (int i = 0; i < mListInventory.size(); i++) {
                String state = mListInventory.get(i).getState();
                if (state != null && state.equals("完成")) {
                    mListOk.add(mListInventory.get(i));
                } else {
                    mListNo.add(mListInventory.get(i));
                }
            }
            mSkedsubAdapter = new TaskSkedsubAdapter(this, mListNo);
            mListView.setAdapter(mSkedsubAdapter);
            mSkedsubAdapter.notifyDataSetChanged();
        }


        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(TaskSubActivity.this, TaskSaninngActivity.class);
                InventoryTaskSub inventoryTaskSub = mListInventory.get(i);
                String id = inventoryTaskSub.getId();
                Bundle bundle = new Bundle();
                bundle.putSerializable("inventoryTaskSub", inventoryTaskSub);
                bundle.putSerializable("id", id);
                bundle.putSerializable("mIntID", mIntID);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }



    @OnClick({R.id.fh, R.id.ztai})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fh:
                finish();
                break;
            case R.id.ztai:
                if(tyme){
                    tyme=false;
                    mSkedsubAdapter.setListDataBean(mListOk);
                    mSkedsubAdapter.notifyDataSetChanged();
                }else {
                    tyme=true;
                    mSkedsubAdapter.setListDataBean(mListNo);
                    mSkedsubAdapter.notifyDataSetChanged();
                }

                break;
        }
    }


    @Override
    protected void onStart() {  // 界面返回走

        intiData();
        Log.v(TAG, "---------------onStart--------------------");
        super.onStart();
    }
    @Override
    protected void onRestart() { //

        Log.v(TAG, "---------------onRestart--------------------");
        super.onRestart();
    }

    @Override
    protected void onResume() {
        Log.v(TAG, "---------------onResume--------------------");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.v(TAG, "---------------onPause--------------------");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.v(TAG, "---------------onStop--------------------");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.v(TAG, "---------------onDestroy--------------------");
        super.onDestroy();
    }
}
